package com.ohuang.test_compose.page

import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember


@Composable
fun ColumnKeyTestPage() {
    val mutableStateListOf = remember {
        mutableStateListOf<String>("aa", "bb", "cc", "dd", "ee", "ff")
    }

    Column() {
        Button(onClick = {
            mutableStateListOf.removeAt(0)
            mutableStateListOf.add("new" + System.currentTimeMillis())
        }) {
            Text(text = "修改列表(删除第一项,底部新增一项)")
        }
        Text(text = "设置了key---  每次修改后 相同key就不会发生重组 所以文字颜色不改变")
        mutableStateListOf.distinct().forEachIndexed { index, s ->
            key(s) {
                ColumnKeyTestPageItem(s)
            }
        }
        Text(text = "没有设置了key---")
        mutableStateListOf.distinct().forEachIndexed { index, s ->
            ColumnKeyTestPageItem(s)
        }
        Text(text = "自己实现的key---  自己根据源码实现的key 没效果")
        mutableStateListOf.distinct().forEachIndexed { index, s ->
            myKey(s) {
                ColumnKeyTestPageItem(s)
            }
        }

    }
}


@Composable
fun ColumnKeyTestPageTest(s: List<String>) {
    s.forEach {
        key(it) {
            ColumnKeyTestPageItem(it)
        }
    }
}

/**
 * 自己实现的key
 */
@Composable
fun ColumnKeyTestPageTestMyKey(s: List<String>) {
    s.forEach {
        myKey(it) {
            ColumnKeyTestPageItem(it)
        }
    }
}

@Composable
fun ColumnKeyTestPageTestNoKey(s: List<String>) {
    s.forEach {
        ColumnKeyTestPageItem(it)
    }
}

@Composable
fun ColumnKeyTestPageItem(s: String) {
    Text(text = s, color = getRandomColor())
}

@Composable
inline fun <T> myKey(
    @Suppress("UNUSED_PARAMETER")
    vararg keys: Any?,
    block: @Composable () -> T
) = block()